<? if (!defined("BASEPATH")) exit("No direct script access allowed");

//Common::pr($getAvailableProgramsBySubject);

if(isset($getAvailableProgramsBySubject["ins"]) and count($getAvailableProgramsBySubject["ins"])) {

	if(!$this->input->is_ajax_request()) echo	"<article class=\"content\" id=\"articleAvailablePrograms\">";
	echo		"<h2 class=\"blue title\">".$this->lang->line("AVAILABLE")."  ".$this->lang->line("PROGRAMS")."</h2>";

	echo		"<div class=\"accordion\">";

	foreach($getAvailableProgramsBySubject["ins"] as $insData) {
		echo			"<article class=\"box\">";

		echo				"<header>";
		echo					"<h3>".$insData["insName"]."</h3>";
		echo					"<address>".$insData["cityName"].", ".$insData["couName"]."</address>";
		echo					"<dl>";
		echo					"<dt>".$this->lang->line("NUMBER_OF_COURSES").":</dt><dd>".count($insData["majors"])."</dd>";
		echo					"</dl>";
		echo				"</header>";

		echo				"<section>";
		echo					"<table class=\"grid\">";
		$elso = TRUE;
		foreach($insData["majors"] as $majData) {
			if($elso) {
				echo						"<thead>";
				echo							"<tr>";
				echo								"<th>".$this->lang->line("COURSE")."</th>";
				echo								"<th>".$this->lang->line("PROGRAM_TYPE")."</th>";
				foreach($majData["cat"] as $catData) {
					/* csak kiemeltek jelentjenek meg */
					if($catData["catHighlight"] == "I") {
						echo								"<th>".$catData["catName"]."</th>";
					}
				}
				echo							"</tr>";
				echo						"</thead>";
				echo						"<tbody>";
				$elso = FALSE;
			}
			echo							"<tr>";
			//echo								"<td>".anchor(site_url().$insData["conUrlName"]."/".$insData["couUrlName"]."/".$insData["cityUrlName"]."/".$insData["insUrlName"]."/".$majData["majUrlName"], $majData["majName"])."</td>";
			echo								"<td>".anchor(site_url()."institutes/".$insData["insUrlName"]."/".$majData["majUrlName"], $majData["majName"])."</td>";
			echo								"<td>".$majData["majProgTypeLname"]."</td>";
			foreach($majData["cat"] as $catData) {
				/* csak kiemeltek jelentjenek meg */
				if($catData["catHighlight"] == "I") {
					echo								"<td>".Common::coalesce(array($catData["e2cInt"], $catData["e2cText"]))."</td>";
				}
			}
			echo							"</tr>";
		}
		echo						"</tbody>";
		echo					"</table>";

		echo				"</section>";

		echo			"</article>";
	}

	echo		"</div>";

	$cnt = $getAvailableProgramsBySubject["cnt"];
	$aktPage = Common::coalesce(array($this->input->post("page"), 1));
	$pageSize = $this->config->item("availableProgramPageSize");
	$maxPage = floor($cnt/$pageSize);
	if($cnt > $pageSize) {

		echo	"
					<script type=\"text/javascript\">
						function jsPager(page) {
							$.ajax({
								type: 'POST',
								url: '".site_url().Common::coalesce(array($this->input->post("current_url"), uri_string()))."',
								data: {current_url: '".Common::coalesce(array($this->input->post("current_url"), uri_string()))."', page: page},
								beforeSend: function(xhr) {
									run = true;
									$('#articleAvailablePrograms').html('<div class=\"box gray\"><p style=\"text-align:center; margin:10px;\"><img src=\"".MEDIA."img/loading.gif\" alt=\"\" /> ".$this->db->escape_str($this->lang->line("PLEASE_WAIT,_PROCESSING_IN_PROGRESS"))."</p><div>');
								}
							}).done(function ( data ) {
									$('#articleAvailablePrograms').html(data);
									run = false;
									accordion();
							});
						}
					</script>
					";

		echo	"<nav class=\"pager\">";

		if($aktPage == 1) {
			echo		"<span class=\"button prev\">".$this->lang->line("PREV")."</span>";
		} else {
			echo		"<a class=\"button prev\" onclick=\"jsPager('".($aktPage - 1)."');\">".$this->lang->line("PREV")."</a>";
		}

		// max 10 number
		echo		"<span class=\"pages\">";
		$dotdotdot = FALSE;
		for($i = 1; $i <= $maxPage; $i++) {

			if($i < 4 or $i > $maxPage-3 or ($i > $aktPage-3 and $i < $aktPage+3)) {
				$dotdotdot = FALSE;
				if($aktPage == $i) {
					echo			"<strong>".$i."</strong>";
				} else {
					echo			"<a onclick=\"jsPager('".$i."');\">".$i."</a>";
				}
			} else {
				if(!$dotdotdot) {
					echo	"...";
					$dotdotdot = TRUE;
				}
			}

		}
		echo		"</span>";

		if($maxPage == $aktPage) {
			echo		"<span class=\"button next\">".$this->lang->line("NEXT")."</span>";
		} else {
			echo		"<a class=\"button next\" onclick=\"jsPager('".($aktPage + 1)."');\">".$this->lang->line("NEXT")."</a>";
		}

		echo	"</nav>";
	}

	if(!$this->input->is_ajax_request()) echo	"</article>";

}